package com.ternnetwork.core.accesscontroll.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import com.ternnetwork.commons.model.http.BaseResponse;
import com.ternnetwork.core.accesscontroll.entity.RescRole;
import com.ternnetwork.core.accesscontroll.entity.RescRoleId;
import com.ternnetwork.core.accesscontroll.entity.Resources;
import com.ternnetwork.core.accesscontroll.enums.ResourcesType;
import com.ternnetwork.core.accesscontroll.repository.RescRoleRepository;
import com.ternnetwork.core.accesscontroll.repository.ResourcesRepository;
import com.ternnetwork.core.accesscontroll.repository.RoleRepository;
import com.ternnetwork.core.accesscontroll.service.RescRoleService;

@Service
public class RescRoleServiceImpl implements RescRoleService {
	
	@Autowired
	private RescRoleRepository rescRoleRepository;
	
	@Autowired
	private ResourcesRepository resourcesRepository;
	
	@Autowired
	private RoleRepository roleRepository;
	
	@Override
	@Transactional(propagation = Propagation.REQUIRES_NEW,rollbackFor=Exception.class)
    public BaseResponse update(Long roleId, ResourcesType resourcesType, String resourcesIds){
		
		BaseResponse res=new BaseResponse();
		
		String[] resourcesIdsArray=StringUtils.isEmpty(resourcesIds)==true?new String[]{}:resourcesIds.split(",");
		
		deleteBatch(roleId, resourcesType, resourcesIdsArray);//删除未选中的
		
		addBatch(roleId, resourcesIdsArray);//添加已选中的
		
		res.setSccucess();
		
		return res;
	}
	
	
	/**
	 * 按角色ID，需要保留的资源ID来删除不需要的资源
	 * @param roleId
	 * @param resourcesType
	 * @param resourcesIdsArray
	 */
	private void deleteBatch(Long roleId,ResourcesType resourcesType, String[] resourcesIdsArray) {
		List<RescRole> allList=rescRoleRepository.findAll(roleId,resourcesType);
		for(RescRole t:allList){
			//如果已有的资源不在resourcesIds中则将其删除
			if(checkExist(t.getResources().getId(),resourcesIdsArray)==false){
				long totalCount=rescRoleRepository.getCountById(t.getId());
				if(totalCount>0L){
					rescRoleRepository.delete(t);
				}
			}
		}
	}
	
	private void addBatch(long roleId, String[] resourcesIdsArray) {
		for(String resourcesIdStr:resourcesIdsArray){
			if(!StringUtils.isEmpty(resourcesIdStr)){
				Long  resourcesId=Long.parseLong(resourcesIdStr);
				RescRoleId id=new RescRoleId();
				id.setResourcesId(resourcesId);
				id.setRoleId(roleId);
				long totalCount=rescRoleRepository.getCountById(id);
				//如果不存在则添加
				if(totalCount==0L){
					RescRole rescRole=new RescRole();
					rescRole.setId(id);
					rescRole.setRole(roleRepository.getOne(roleId));
					rescRole.setResources(resourcesRepository.getOne(resourcesId));
					rescRoleRepository.saveAndFlush(rescRole);
				}
			}
		}
	}
	
	
	private Boolean checkExist(long resourcesId,String[] resourcesIdsArray){
		for(String resourcesIdStr:resourcesIdsArray){
			if(Long.parseLong(resourcesIdStr)==resourcesId){
				return true;
			}
		}
		return false;
	}
	
	
	
	
	
	
	
	
	public List<Resources> findAllChild(List<Resources> oriList,long id){
		Resources tr=new Resources();
		tr.setId(id);
		oriList.add(tr);
		List<Resources> list=resourcesRepository.findAllByParentId(id);
		for(Resources t:list){
			oriList.addAll(findAllChild(oriList,t.getId()));
		}
		return oriList;
	}
	
	
	public List<Resources> findAllParent(List<Resources> oriList,long id){
		Resources t=resourcesRepository.getOne(id);
		oriList.add(t);
		if(t.getParentId()!=0){
			oriList.addAll(findAllParent(oriList,t.getParentId()));
		}
		return oriList;
	}
	
	
	@Override
	public void deleteBatch(long resourcesId,String deleteRoleIds){
		if(StringUtils.isEmpty(deleteRoleIds)) {
			return ;
		}
		String[] deleteRoleId=deleteRoleIds.split(",");
		List<Resources> list=findAllChild(new ArrayList<Resources>(),resourcesId);
		for(String roleId:deleteRoleId){
			for(Resources t:list){
				RescRoleId id=new RescRoleId();
				id.setResourcesId(t.getId());
				id.setRoleId(Long.parseLong(roleId));
				rescRoleRepository.deleteAllById(id);
			}
		}
	}
	
	
	@Override
	public void addBatch(long resourcesId,String addRoleIds){
		if(StringUtils.isEmpty(addRoleIds)) {
			return ;
		}
		String[] addRoleId=addRoleIds.split(",");
    	List<Resources> list=findAllParent(new ArrayList<Resources>(),resourcesId);
		for(String roleId:addRoleId){
			for(Resources t:list){
				RescRoleId id=new RescRoleId();
				id.setResourcesId(t.getId());
				id.setRoleId(Long.parseLong(roleId));
				long totalCount=rescRoleRepository.getCountById(id);
				if(totalCount==0L) {
					RescRole rescRole=new RescRole();
					rescRole.setId(id);
					rescRoleRepository.saveAndFlush(rescRole);
				}
			}
		}
   	}
	
	
	
	
}
